package dk.itu.samd.group2.tagapastry;
import java.util.ArrayList;
import java.util.HashMap;



public class TaggingBlackBox {

	private HashMap<Tag, ArrayList<String>> taggedItems;
	private boolean allowAlways;
	
	public TaggingBlackBox(boolean allowAlways) {
		this.allowAlways = allowAlways;
		if (!allowAlways)
			taggedItems = new HashMap<Tag, ArrayList<String>>();
	}
	
	public boolean shouldUntag(Tag tag, String obj) {
		if (allowAlways) {
			return true;
		}
		if (!taggedItems.containsKey(tag)) {
			return false;
		} else if (taggedItems.get(tag).contains(obj)) {
			taggedItems.get(tag).remove(obj);
			if (taggedItems.get(tag).isEmpty()) {
				taggedItems.remove(tag);
			}
			return true;
		}
		return false;
	}
	
	public boolean shouldTag(Tag tag, String obj) {
		if (allowAlways) {
			return true;
		}
		if (!taggedItems.containsKey(tag)) {
			ArrayList<String> list = new ArrayList<String>();
			list.add(obj);
			taggedItems.put(tag, list);
			return true;
		} else if (taggedItems.get(tag).contains(obj)) {
			return false;
		} else {
			taggedItems.get(tag).add(obj);
			return true;
		}
	}
	
}
